<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<p>* 표시는 필수 입력 사항입니다.</p>
<form id="form1" name="form1" method="post" action="">
	<fieldset>
		<table summary="회원가입양식">
			<tbody>
				<tr>
					<th scope="row" align="left">
						* 아이디(이메일)
					</th>
					<td>
						<c:if test="${member.seq != null }">
							<input type="hidden" id="seq" name="seq" value="${member.seq }"/>
						</c:if>
						<input type="text" id="id" name="id" value="${member.id}" />
					</td>
					<th scope="row" align="left">
						<!--input type="image" alt="이메일 인증하기"-->
					</th>									
				</tr>
				<tr>
					<th scope="row" align="left">
						* 이름
					</th>
					<td>
						<input type="text" id="name" name="name" value="${member.name}" />
					</td>				
				</tr>
				<tr>
					<th scope="row" align="left">
						* 비밀번호
					</th>
					<td>
						<input type="password" name="password" id="password"/>
					</td>				
				</tr>
				<tr>
					<th scope="row" align="left">
						* 비밀번호 확인
					</th>
					<td>
						<input type="password" name="passwordCheck" id="passwordCheck" />
					</td>				
				</tr>
				<tr>
					<th scope="row" align="left">
						* 성별
					</th>
					<td>
						<input type="hidden" name="sex" id="sex" value=""/>
						<c:if test="${member.sex == 'M' }">
						<input type="radio" name="optSex" id="optSex" value="M" checked />남자
						<input type="radio" name="optSex" id="optSex" value="F" />여자
						</c:if>
						<c:if test="${member.sex == 'F' }">
						<input type="radio" name="optSex" id="optSex" value="M" />남자
						<input type="radio" name="optSex" id="optSex" value="F" checked />여자
						</c:if>
						<c:if test="${member.sex == null }">
						<input type="radio" name="optSex" id="optSex" value="M" />남자
						<input type="radio" name="optSex" id="optSex" value="F" />여자
						</c:if>
					</td>				
				</tr>
				<tr>
					<th scope="row" align="left">
						 생년월일
					</th>
					<td>
						<input type="hidden" name="birthday" id="birthday" value=""/>
						<input type=text name="year" id="year" maxlength="4" size="5" value="${fn:substring(member.birthday,0,4)}"/>년
						<select name="month">
							<c:forEach var="i" begin="1" end="12" step="1">
							<c:if test="${i<10 }">
								<c:set var="smon" value="${fn:substring(member.birthday,5,6)}"/>
								<option value="0${i}" <c:if test="${smon == i}">selected</c:if>>0${i}</option>
							</c:if>
							<c:if test="${i>9 }">
								<c:set var="smon" value="${fn:substring(member.birthday,4,6)}"/>
								<option value="${i}" <c:if test="${smon == i}">selected</c:if>>${i}</option>
							</c:if>	
							</c:forEach>
						</select>월
						<select name="day" >
							<c:forEach var="i" begin="1" end="31" step="1">
							<c:if test="${i<10 }">
								<c:set var="sday" value="${fn:substring(member.birthday,7,8)}"/>
								<option value="0${i}" <c:if test="${sday == i}">selected</c:if> >0${i}</option>
							</c:if>
							<c:if test="${i>9 }">
								<c:set var="sday" value="${fn:substring(member.birthday,6,8)}"/>
								<option value="${i}" <c:if test="${sday == i}">selected</c:if> >${i}</option>
							</c:if>	
							</c:forEach>
						</select>일
					</td>				
				</tr>
				<tr>
					<th scope="row" align="left">
						* 이메일 수신동의 
					</th>
					<td>
						<input type="hidden" name="emailAgreeYn" id="emailAgreeYn" value=""/>
						<input type="radio" name="optEmailAgreeYn" id="optEmailAgreeYn" value="Y" <c:if test="${member.emailAgreeYn =='Y' }"> checked</c:if> />Y
						<input type="radio" name="optEmailAgreeYn" id="optEmailAgreeYn" value="N" <c:if test="${member.emailAgreeYn =='N' }"> checked</c:if>/>N
					</td>				
				</tr>
				<tr>
					<th scope="row" align="left">
						* 개인정보 활용동의 
					</th>
					<td>
						<input type="hidden" name="personalAgreeYn" id="personalAgreeYn" value=""/>
						<input type="radio" name="optPersonalAgree" id="optPersonalAgree" value="Y" <c:if test="${member.personalAgreeYn =='Y' }"> checked</c:if>/>Y
						<input type="radio" name="optPersonalAgree" id="optPersonalAgree" value="N" <c:if test="${member.personalAgreeYn =='N' }"> checked</c:if>/>N
					</td>				
				</tr>
			</tbody>
		</table>
	</fieldset>
</form> 
	<div>
		<c:if test="${member.id == null }">
		<button onclick="joinMember();" style="font-size:12px;font-family: Malgun Gothic,Gulim;padding:0;margin:0">확인</button>
		</c:if>
		<c:if test="${member.id != null }">
		<button onclick="updateMember();" style="font-size:12px;font-family: Malgun Gothic,Gulim;padding:0;margin:0">수정</button>
		</c:if>
		<button onclick="goMain();" style="font-size:12px;font-family: Malgun Gothic,Gulim;padding:0;margin:0">취소</button>
	</div>	
<script type="text/javascript">
	var form1 = document.form1;		
	
	var joinMember = function(){
		var a = checkAll();
		if(a == true){
			form1.action ="/member/join.html"
			form1.submit();
		}
		
	}
	
	var updateMember = function(){
		var a = checkAll();
		if(a == true){
			form1.action ="/member/update.html"
			form1.submit();
		}
		
	}
	
	var goMain = function(){
		var x = confirm("취소하시겠습니까?");
		
		if( x == true){
			location.href = "/main.html"; 
			location.replace("/main.html"); 
		}
	}
	var checkValue = function(obj){
		
	}
	
	var checkAll = function(){
		for(i=0;i<2;i++){
			if(form1.optSex[i].checked == true) form1.sex.value = form1.optSex[i].value;
			if(form1.optEmailAgreeYn[i].checked == true) form1.emailAgreeYn.value = form1.optEmailAgreeYn[i].value;
			if(form1.optPersonalAgree[i].checked == true) form1.personalAgreeYn.value = form1.optPersonalAgree[i].value;
		}
		
		form1.birthday.value = form1.year.value + form1.month.value + form1.day.value;
		
		if(form1.id.value == null || form1.id.value == ''){ 
			alert("아이디를 입력하세요"); 
			return false;
		}
		else if(form1.name.value == null || form1.name.value == ''){ 
			alert("이름을 입력하세요"); 
			return false;
		}
		else if(form1.password.value == null || form1.password.value == ''){
			alert("비밀번호를 입력하세요"); 
			return false;
		}
		else if(form1.passwordCheck.value == null || form1.passwordCheck.value == ''){
			 alert("비밀번호 확인를 입력하세요"); 
			 return false;
		}
		else if(form1.password.value != form1.passwordCheck.value){
			 alert("비밀번호가 다릅니다"); 
			 return false;
		}
		else if(form1.birthday.value == null || form1.birthday.value == ''){
			alert("생년월일 입력하세요"); 
			return false;
		} 
		else if(form1.sex.value == null || form1.sex.value == ''){
			 alert("성별을 선택하세요"); 
			 return false;
		}
		else if(form1.emailAgreeYn.value == null || form1.emailAgreeYn.value == ''){
			alert("이메일수신동의를 해주세요"); 
			return false;
		}  
		else if(form1.personalAgreeYn.value == null || form1.personalAgreeYn.value == '' || form1.personalAgreeYn.value == 'N'){
			  alert("개인정보동의를 해주세요"); 
			  return false;
		}
		
		else return true;
		
	}
</script>
